/*
*连接数据库
 */
var mysql = require('mysql')

//创建连接池
var pool = mysql.createPool({
    connectionLimit: 100,//最大连接数
    multipleStatements: true,//允许多语句查询
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'test'
})

//用户列表
module.exports.userList = function (callback) {
    pool.getConnection(function (err, conn) {
        if (err) {
            return callback("连接池连接失败！" + err, null)
        }
        var sql = `SELECT * FROM users ;`
        conn.query(sql, function (err, results) {
            conn.release()
            if (err) {
                return callback("查询失败！" + err, null)
            }
            callback(null, results)
        })
    })
}

//用户
module.exports.getUserById = function (id, callback) {
    pool.getConnection(function (err, conn) {
        if (err) {
            return callback("连接池连接失败！" + err, null)
        }
        var sql = `SELECT * FROM users where id =? ;`
        conn.query(sql, [id], function (err, results) {
            conn.release()
            if (err) {
                return callback("查询失败！" + err, null)
            }
            callback(null, results)
        })
    })
}

//刪除
module.exports.delUser = function (id, callback) {
    pool.getConnection(function (err, conn) {
        if (err) {
            return callback("连接池连接失败！" + err, null)
        }
        var sql = `delete FROM users where id = ? ;`
        conn.query(sql, [id], function (err, results) {
            conn.release()
            if (err) {
                return callback("刪除失败！" + err, null)
            }
            callback(null, results)
        })
    })
}


//根据电话查找用户信息
module.exports.findPhone = function (phone, callback) {
    pool.getConnection(function (err, conn) {
        if (err) {
            return callback("连接池连接失败！" + err, null)
        }
        var sql = "select * from user_info where phone=?"
        conn.query(sql, [phone], function (err, results) {
            conn.release()
            if (err) {
                return callback("查询失败！" + err, null)
            }
            callback(null, results)
        })
    })
}

//注册：将信息存储到数据表中
module.exports.addUser = function (data, callback) {
    console.log(data)
    pool.getConnection(function (err, conn) {
        if (err) {
            console.log(err)
            return callback("连接池连接失败！" + err, null)
        }
        var sql = 'insert into users set ?'
        conn.query(sql, data, function (err, results) {
            conn.release()
            if (err) {
                return callback("查询失败！" + err, null)
            }
            callback(null, results)
        })
    })
}

//修改
module.exports.updateUser = function (data, id, callback) {
    pool.getConnection(function (err, conn) {
        if (err) {
            return callback("连接池连接失败！" + err, null)
        }
        var sql = 'UPDATE users SET ? WHERE id = ?'
        conn.query(sql, [data, id], function (err, results) {
            conn.release()
            if (err) {
                return callback("修改失败！" + err, null)
            }
            callback(null, results)
        })
    })
}

//查询某商品详情
module.exports.findOne = function (p_id, callback) {
    pool.getConnection(function (err, conn) {
        if (err) {
            return callback("连接池连接失败！" + err, null)
        }
        var sql = `SELECT * FROM go_product WHERE p_type='ad-product-computer' LIMIT 4;
                SELECT * FROM go_product WHERE p_type='ad-product-phone' LIMIT 4;
                SELECT * FROM go_product WHERE p_type='ad-product-pad' LIMIT 4;
                SELECT * FROM go_product WHERE p_type='ad-product-ear' LIMIT 4;
                SELECT * FROM go_product WHERE p_id=? ;
               `
        conn.query(sql, [p_id], function (err, results) {
            conn.release()
            if (err) {
                return callback("查询失败！" + err, null)
            }
            callback(null, results)
        })
    })
}